def is_valid_ip(ip):
    parts = ip.split('.')
    if len(parts) != 4:
        return False
    for part in parts:
        # 检查是否为空或包含非数字字符
        if not part.isdigit():
            return False
        # 检查前导零
        if len(part) > 1 and part[0] == '0':
            return False
        # 转换为整数并验证范围
        num = int(part)
        if num < 0 or num > 255:
            return False
    return True

# 获取用户输入并验证
ip_address = input("请输入IP地址：")
print("有效IP" if is_valid_ip(ip_address) else "无效IP")

